package com.finger.forest;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureCollection;
import com.esri.arcgisruntime.data.FeatureCollectionTable;
import com.esri.arcgisruntime.data.Field;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureCollectionLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.finger.forest.base.CommonKey;
import com.finger.forest.utils.ClickUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GisFeatureCollectionActivity extends AppCompatActivity {

    private MapView mMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gis_feature_collection);

        mMapView = findViewById(R.id.mapView);
        setupMap();

        createFeatureCollection();

    }

    private void createFeatureCollection() {
        if (mMapView != null) {
            FeatureCollection featureCollection = new FeatureCollection();
            FeatureCollectionLayer featureCollectionLayer = new FeatureCollectionLayer(featureCollection);
            mMapView.getMap().getOperationalLayers().add(featureCollectionLayer);
            createPointTable(featureCollection);
        }
    }

    private void createPointTable(FeatureCollection featureCollection) {
        List<Field> pointFields = new ArrayList<>();
        pointFields.add(Field.createDate("name","n"));

        FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields,GeometryType.POINT, SpatialReferences.getWgs84());
        featureCollection.getTables().add(pointsTable);

        List<Feature> features = new ArrayList<>();
        // *** ADD ***
        // Malibu Pier
        Map<String, Object> attributes1 = new HashMap<>();
        attributes1.put(pointFields.get(0).getName(), "Malibu Pier");
        Point point1 = new Point(-118.676726, 34.037288, SpatialReferences.getWgs84());
        features.add(pointsTable.createFeature(attributes1, point1));

        // Hindi Temple
        Map<String, Object> attributes2 = new HashMap<>();
        attributes2.put(pointFields.get(0).getName(), "Malibu Hindi Temple");
        Point point2 = new Point(-118.709726, 34.095097, SpatialReferences.getWgs84());
        features.add(pointsTable.createFeature(attributes2, point2));

        // Escondido Falls
        Map<String, Object> attributes3 = new HashMap<>();
        attributes3.put(pointFields.get(0).getName(), "Escondido Falls");
        Point point3 = new Point(-118.779438, 34.044211, SpatialReferences.getWgs84());
        features.add(pointsTable.createFeature(attributes3, point3));

        // *** ADD ***
        pointsTable.addFeaturesAsync(features);

    }

    private void setupMap() {
        if (mMapView != null) {
            ArcGISRuntimeEnvironment.setLicense(CommonKey.LICENSE);
            mMapView.setAttributionTextVisible(false);
            Basemap.Type basemapType = Basemap.Type.IMAGERY;
            double latitude = 34.0270;
            double longitude = -118.8050;
            //int levelOfDetail = 13;
            int levelOfDetail = 14;
            ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
            mMapView.setMap(map);
        }
    }

}
